<?php
require_once 'EZend/Controller/AdminBaseController.php';

class Admin_ManagerController extends AdminBaseController {
	public function init() {
		parent::init();
		$this->_helper->layout->setLayout('iframe');
	}
	public function indexAction() {
		$this->view->assign('managers', $this->getAdminUserService()->findAll());
	}
	
	public function editAction() {
		$managerId = intval($this->getRequest()->getParam('managerId'));
		if ($managerId < 1) return $this->showNotice('非法操作');
		$data = $this->getAdminUserService()->get($managerId);
		if (!$data) return $this->showNotice('找不到用户');
		
		$this->view->username = $data['user_name'];
	}
	
	public function doeditAction() {
		$managerId = intval($this->getRequest()->getParam('managerId'));
		if ($managerId < 1) return $this->showNotice('非法操作');
		$data = $this->getAdminUserService()->get($managerId);
		if (!$data) return $this->showNotice('找不到用户');
		
		if ($data['is_god'] == 1) return $this->showNotice('该管理员不允许被编辑');
		
		$password = trim((string) $this->getRequest()->getPost('password'));
		$checkPwd = trim((string) $this->getRequest()->getPost('check_pwd'));
		
		$isUpdate = false;
		if ($password != '') {
			if (!preg_match('/^[0-9A-Za-z]{5,20}$/', $password)) return $this->showNotice('密码不规范');
			if ($password != $checkPwd) return $this->showNotice('密码前后不一致');
			$isUpdate = $this->getAdminUserService()->update($managerId, array('password' => $password));
		}
		if (!$isUpdate) return $this->showNotice('您未作任何修改');
		return $this->showNotice('编辑用户成功', $this->getInnerUrl(array('action' => 'index')));
	}
	
	public function deleteAction() {
		$managerId = intval($this->getRequest()->getParam('managerId'));
		if ($managerId < 1) return $this->showNotice('非法操作');
		$data = $this->getAdminUserService()->get($managerId);
		
		if ($data['is_god'] == 1) return $this->showNotice('该管理员不允许被删除');
		
		if (!$this->getAdminUserService()->delete($managerId)) return $this->showNotice('删除用户失败');
		return $this->showNotice('删除用户成功', $this->getInnerUrl(array('action' => 'index')));
	}
	
	public function addAction() {
		$data['user_name'] = $this->getRequest()->getPost('username');
		$data['password'] = $this->getRequest()->getPost('password');
		$checkPwd = $this->getRequest()->getPost('check_pwd');
		
		if ('' == $data['user_name'] || '' == $data['password']) return $this->showNotice('用户名或密码不能为空');
		if ($data['password'] != $checkPwd) return $this->showNotice('密码前后不一致');
		if ($this->getAdminUserService()->isUserNameExist($data['user_name']) != false) return $this->showNotice('用户名已经存在');
		if (!preg_match('/^[0-9A-Za-z_]{5,20}$/', $data['user_name']) || !preg_match('/^[0-9A-Za-z]{5,20}$/', $data['password'])) return $this->showNotice('用户名密码不合规范');
		$data['is_valid'] = 1;
		
		if (!$this->getAdminUserService()->add($data)) return $this->showNotice('添加管理员失败');
		return $this->showNotice('添加管理员成功', $this->getInnerUrl(array('action' => 'index')));
	}
	
	protected function getAdminUserService() {
		require_once 'Models/Admin/Service/AdminServiceFactory.php';
		return AdminServiceFactory::getInstance()->createAdminUserService();
	}
}
